/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.web.admin;

import com.jfinal.aop.Inject;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.validate.EmptyValidate;
import io.jboot.web.validate.Form;
import io.jpress.JPressConsts;
import io.jpress.commons.layer.SortKit;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.model.Department;
import io.jpress.permission.annotation.PermissionDef;
import io.jpress.service.DepartmentService;
import io.jpress.service.MenuService;
import io.jpress.web.AdminPermissionGroups;
import io.jpress.web.base.AdminControllerBase;

import java.util.List;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 * @Package io.jpress.module.article.admin
 */
@RequestMapping(value = "/admin/department", viewPath = JPressConsts.DEFAULT_ADMIN_VIEW)
public class _DepartmentController extends AdminControllerBase {

    @Inject
    private DepartmentService departmentService;

    @Inject
    private MenuService menuService;


    /**
     * 机构部门列表
     */
    @PermissionDef(title = "机构部门管理", groupId = AdminPermissionGroups.USER)
    @AdminMenu(text = "机构部门", groupId = JPressConsts.SYSTEM_MENU_USER, order = 3)
    public void list() {

        List<Department> departments = departmentService.findAll();
        SortKit.toLayer(departments);
        setAttr("departments", departments);


        long id = getParaToLong(0, 0L);
        if (id > 0 && departments != null) {
            for (Department department : departments) {
                if (department.getId().equals(id)) {
                    setAttr("department", department);
                }
            }
        }

        render("department/department_list.html");
    }


    /**
     * 保存部门信息
     */
    @PermissionDef(title = "修改部门", groupId = AdminPermissionGroups.USER)
    @EmptyValidate({
            @Form(name = "department.name", message = "部门（机构）名称不能为空"),
    })
    public void doSave() {
        Department department = getModel(Department.class, "department");
        Object id = departmentService.saveOrUpdate(department);
        renderOkJson("id", id);
    }


    /**
     * 删除部门信息
     */
    @PermissionDef(title = "删除部门", groupId = AdminPermissionGroups.USER)
    public void doDel() {
        Long id = getIdPara();
        if (id == 1) {
            renderFailJson("总部不能被删除！");
            return;
        }
        render(departmentService.deleteById(id) ? OK : FAIL);
    }


}
